package com.hkzhao.reportingsystem.controller;

import com.hkzhao.reportingsystem.common.api.CommonPage;
import com.hkzhao.reportingsystem.common.api.CommonResult;
import com.hkzhao.reportingsystem.common.utils.TokenTranslate;
import com.hkzhao.reportingsystem.common.utils.TranslateToToken;
import com.hkzhao.reportingsystem.dto.AdminLoginParam;
import com.hkzhao.reportingsystem.dto.CaseDto;
import com.hkzhao.reportingsystem.mbg.model.Doctor;
import com.hkzhao.reportingsystem.mbg.model.News;
import com.hkzhao.reportingsystem.service.MangerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "ManagerController",description = "管理人员控制器")
@RequestMapping("/manage")
@Controller
public class ManagerController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired(required = false)
    private MangerService mangerService;
    @ApiOperation("登录以后返回token")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody AdminLoginParam adminLoginParam, BindingResult bindingResult)
    {
        String token = mangerService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword());
        if(token==null)
            return CommonResult.validateFailed("用户名或者密码错误");
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return CommonResult.success(tokenMap);
    }
    @ApiOperation("插入新闻")
    @RequestMapping(value = "/addNews",method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('manager:write_and_read')")
    public CommonResult addNews(@RequestParam @ApiParam("标题") String title, @RequestParam @ApiParam("内容") String text, HttpServletRequest request){
        if(title.length()==0)
            return CommonResult.failed("标题不能为空");
        return mangerService.addNews(title,text,request);
    }
    @ApiOperation("审核成功与失败的接口 String:1为成功，String:0为失败")
    @ResponseBody
    @RequestMapping(value = "/audit",method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('manager:write_and_read')")
    public CommonResult audit(@RequestParam(value = "username") @ApiParam("该用户名") String username,
                              @RequestParam(value = "target") @ApiParam("标志量表示成功与否")String target){
        if(target.equals("1")) //陈坤
        {
            return mangerService.verifySuccess(username);

        }
        else if(target.equals("0")) //失败
        {
            return mangerService.verifyFailed(username);
        }
        else
        {
            return CommonResult.failed("标志量或用户名不能为空");
        }
    }
    @ApiOperation("返回分页后的待审核医生 pageNum+pageSize 前端自己弄个计数器看更新请求就好了")
    @ResponseBody
    @RequestMapping(value = "/getNeedVerifyDoctor",method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('manager:write_and_read')")
    public CommonResult<CommonPage<Doctor>> getNews(@RequestParam(value = "pageNum") @ApiParam("页码") int pageNum,
                                                    @RequestParam(value = "pageSize") @ApiParam("页面大小") int pageSize)
    {
        List<Doctor> list = mangerService.getNeedVerifyDoctor(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }
    @ApiOperation("得到待审核的案例信息列表")
    @ResponseBody
    @RequestMapping(value = "/getAllAuthCase",method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('manager:write_and_read')")
    public CommonResult getAllAuthCase(@RequestParam(value = "pageNum") @ApiParam("页码") int pageNum,
                                                            @RequestParam(value = "pageSize") @ApiParam("页面大小") int pageSize)
    {
        List<CaseDto> list = mangerService.getAllAuthCase(pageNum, pageSize);
        return CommonResult.success(list,"手动分页成功");
    }
    @ApiOperation("判断待审的案件信息是否通过")
    @ResponseBody
    @RequestMapping(value = "/authCase",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('manager:write_and_read')")
    public CommonResult authCase(@RequestParam(value = "case_token_id") @ApiParam("案件的唯一标识token") String case_token_id,
                                 @RequestParam(value = "target") @ApiParam("1代表成功，0代表不通过")String target)
    {
        return mangerService.authCaseSuccessOrFailed(case_token_id, target);
    }
    @ApiOperation("随心所欲删除评论")
    @ResponseBody
    @RequestMapping(value = "/deleteCommentById",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('manager:write_and_read')")
    public CommonResult deleteCommentById(@RequestParam(value = "comment_id") @ApiParam("comment_id") String comment_id)
    {
        return mangerService.deleteComment(comment_id);
    }


}
